package com.itzxx.library_base_ktx.base.viewmodel

import android.app.Activity
import android.content.Context
import androidx.lifecycle.ViewModel
import com.itzxx.library_base_ktx.callback.livedata.event.EventLiveData

/**
 * 作者　: hegaojian
 * 时间　: 2019/12/12
 * 描述　: ViewModel的基类 使用ViewModel类，放弃AndroidViewModel，原因：用处不大 完全有其他方式获取Application上下文
 */
open class BaseViewModel : ViewModel() {

    val loadingChange: UiLoadingChange by lazy { UiLoadingChange() }


    val baseContext = EventLiveData<Context>()


    val toastEventLiveData = EventLiveData<String>()


    /**
     * 内置封装好的可通知Activity/fragment 显示隐藏加载框 因为需要跟网络请求显示隐藏loading配套才加的，不然我加他个鸡儿加
     */
    inner class UiLoadingChange {
        //显示加载框
        val showDialog by lazy { EventLiveData<String>() }

        //隐藏
        val dismissDialog by lazy { EventLiveData<Boolean>() }
    }

    fun finish() {
        baseContext.value?.let {
            if (it is Activity) {
                (it).finish()
            }
        }
    }


    open fun onCreateViewModel() {
    }

    open fun onResumeViewModel() {
    }

    open fun onPauseViewModel() {
    }

    open fun onStopViewModel() {
    }

    open fun onDestroyViewModel() {
    }

    /**
     * fragment才会回调
     */
    open fun onHiddenChanged(hidden: Boolean)  {
    }

    fun toast(msg: String) {
        toastEventLiveData.postValue(msg)
    }
}